package com.BinarySheep.eTools;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class ResistorView extends View {

	public Handler updateHandler;
	
	// Drawables
	private Drawable mResback = null;
	private int resbackWidth = 0;
	private int resbackHeight = 0;
	
	// Bands
	private final static int[] BAND_COLORS = new int[] {
		Color.rgb(0x00, 0x00, 0x00), // black
		Color.rgb(0x96, 0x4B, 0x00), // brown		
		Color.rgb(0xFF, 0x00, 0x00), // red
		Color.rgb(0xFF, 0x7F, 0x00), // orange
		Color.rgb(0xFF, 0xFF, 0x00), // yellow
		Color.rgb(0x00, 0x80, 0x00), // green
		Color.rgb(0x00, 0x00, 0xFF), // blue
		Color.rgb(0xEE, 0x82, 0xEE), // violet
		Color.rgb(0x80, 0x80, 0x80), // grey
		Color.rgb(0xFF, 0xFF, 0xFF), // white
		Color.rgb(0xFF, 0xD7, 0x00), // gold
		Color.rgb(0xC0, 0xC0, 0xC0)  // silver
	};
	private final static int[] BAND_TEXT_COLORS = new int[] {
		Color.rgb(0xFF, 0xFF, 0xFF),  // black - white
		Color.rgb(0xFF, 0xFF, 0xFF),  // brown - white
		Color.rgb(0xFF, 0xFF, 0xFF),  // red - white
		Color.rgb(0x00, 0x00, 0x00),  // orange - black
		Color.rgb(0x00, 0x00, 0x00),  // yellow - black
		Color.rgb(0xFF, 0xFF, 0xFF),  // green - white
		Color.rgb(0xFF, 0xFF, 0xFF),  // blue - white
		Color.rgb(0xFF, 0xFF, 0xFF),  // violet - white
		Color.rgb(0x00, 0x00, 0x00),  // grey - black
		Color.rgb(0x00, 0x00, 0x00),  // white - black
		Color.rgb(0x00, 0x00, 0x00),  // gold - black
		Color.rgb(0x00, 0x00, 0x00),  // silver - black
	};
	private final static String[] BAND_TEXT = new String[] {
		"black",
		"brown",
		"red",
		"orange",
		"yellow",
		"green",
		"blue",
		"violet",
		"grey",
		"white",
		"gold",
		"silver"
	};
	public final static int BAND_MODE_4 = 4;
	public final static int BAND_MODE_5 = 5;
	public int bandMode = BAND_MODE_4;
	public int[] bands = null;
	
	// Band setup
	private final static int BAND_X_OFFSET = 57;
	private final static int BAND_Y_OFFSET = 110;
	private final static int BAND_WIDTH = 66;
	private final static int BAND_HEIGHT = 20;
	private final static int BAND_SPACING_4 = 20;
	private final static int BAND_SPACING_5 = 10;
	
	Context context; 
	
	public ResistorView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		this.context = context;
		
		// Load resistor backdrop
		mResback = context.getResources().getDrawable(R.drawable.resback);
		resbackWidth = mResback.getIntrinsicWidth();
		resbackHeight = mResback.getIntrinsicHeight();
		
		SetBandMode(BAND_MODE_4);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		// Draw resistor backdrop
		mResback.setBounds(0, 0, resbackWidth, resbackHeight);
		mResback.draw(canvas);
		
		// Draw bands		
		Paint p, p2;
		Rect band;
		int offset = 0;
		for (int i = 0; i < bandMode; i++) {
			p = new Paint();
			p.setColor(BAND_COLORS[bands[i]]);
			p2 = new Paint();
			p2.setColor(BAND_TEXT_COLORS[bands[i]]);
			p2.setTextSize(16);
			p2.setStrokeWidth(1f);
			p2.setAntiAlias(true);
			p2.setTextAlign(Paint.Align.CENTER);
			
			// Setup band rectangle
			band = new Rect();
			band.left = BAND_X_OFFSET;
			band.right = BAND_X_OFFSET + BAND_WIDTH;			
			band.top = BAND_Y_OFFSET + offset;
			band.bottom = BAND_Y_OFFSET + BAND_HEIGHT + offset;
			
			// Setup offset, skip 3rd band if in 4 band mode
			if (bandMode == BAND_MODE_4)
				offset += BAND_SPACING_4 + BAND_HEIGHT; // move to next space
			else
				offset += BAND_SPACING_5 + BAND_HEIGHT; // move to next space
			
			// Draw that mess
			canvas.drawRect(band, p);			
			canvas.drawText(BAND_TEXT[bands[i]], band.centerX(), band.centerY() + BAND_HEIGHT / 4, p2);			
		}	
	}
	
	private final static int FAT_FINGERS = 5;
	private int IsButtonPressed(float x, float y) {		
		if (x >= BAND_X_OFFSET && x <= (BAND_X_OFFSET + BAND_WIDTH)) {
			int y1 = BAND_Y_OFFSET;
			int y2 = y1 + BAND_HEIGHT;			
			for (int i = 0; i < bandMode; i++) {
				if (y >= y1 - FAT_FINGERS && y <= y2 + FAT_FINGERS)
					return i;
				if (bandMode == BAND_MODE_4) {
					y1 += BAND_SPACING_4 + BAND_HEIGHT;					
					y2 += BAND_SPACING_4 + BAND_HEIGHT;					
				} else {
					y1 += BAND_SPACING_5 + BAND_HEIGHT;
					y2 += BAND_SPACING_5 + BAND_HEIGHT;
				}				
			}			
		}		
		return -1;
	}
	
	private final static String[] BAND_SELECT = new String[] {
		"black",
		"brown",
		"red",
		"orange",
		"yellow",
		"green",
		"blue",
		"violet",
		"grey",
		"white"
	};
	private final static String[] MULT_SELECT = new String[] {
		"black",
		"brown",
		"red",
		"orange",
		"yellow",
		"green",
		"blue",
		"violet",
		"grey",
		"white",
		"gold",
		"silver"
	};
	private final static String[] TOL_SELECT = new String[] {
		"brown",
		"red",
		"green",
		"blue",
		"violet",
		"grey",
		"gold",
		"silver"
	};
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int action = event.getAction();		 
		if (action == MotionEvent.ACTION_DOWN) {
			float posx = event.getX();
			float posy = event.getY();

			// Did we press a button
			final int ret = IsButtonPressed(posx, posy);
			if (ret < 0)
				return true;
			
			// Popup dialog to choose band color
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			if (ret < (bandMode - 2)) {
				builder.setTitle("Pick band #" + (ret + 1) + " color");
				// BAND_SELECT
				builder.setItems(BAND_SELECT, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int item) {
						// Set resulting index to selected band
						bands[ret] = item;
						invalidate();
						// Send update message to handler in Resistor
						Message m = new Message();
						updateHandler.sendMessage(m);			
					}
				});
			} else if (ret == (bandMode - 2)) {
				builder.setTitle("Pick multiplier band");
				// MULT_SELECT
				builder.setItems(MULT_SELECT, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int item) {
						// Set resulting index to selected band
						bands[ret] = item;
						invalidate();
						// Send update message to handler in Resistor
						Message m = new Message();
						updateHandler.sendMessage(m);			
					}
				});
			} else if (ret == (bandMode - 1)) {
				builder.setTitle("Pick tolerance band");
				// TOL_SELECT
				builder.setItems(TOL_SELECT, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int item) {
						// Set resulting index to selected band
						if (item < 2)
							bands[ret] = item + 1;
						else if (item < 6)
							bands[ret] = item + 3;
						else
							bands[ret] = item + 4;
						invalidate();
						// Send update message to handler in Resistor
						Message m = new Message();
						updateHandler.sendMessage(m);			
					}
				});
			}
			AlertDialog alert = builder.create();
			alert.show();
		}
		return true;
	}
	
	public void Clear() {
		SetBandMode(bandMode);
	}
	
	public void SetBandMode(int mode) {
		// Setup bands array
		bandMode = mode;
		bands = new int[bandMode];
	}
	
	public void Update() {
		invalidate();
	}	
	
}
